package com.pengxh.app.gb28181.ui;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.pengxh.androidx.lite.kit.ContextKit;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

public class PermissionActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
    private static final String TAG = "PermissionActivity";
    private static final String[] BASE_PERMISSIONS = {
            Manifest.permission.CAMERA,
            Manifest.permission.RECORD_AUDIO
    };

    private static final String[] STORAGE_PERMISSIONS = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE
    };

    private static final int REQUEST_CODE_PERMISSION = 999;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 添加基础权限
        List<String> permissionsList = new ArrayList<>(Arrays.asList(BASE_PERMISSIONS));

        // Android 9 (API 28) 及以下版本需要存储权限
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
            permissionsList.addAll(Arrays.asList(STORAGE_PERMISSIONS));
        }

        String[] requiredPermissions = permissionsList.toArray(new String[0]);

        if (!EasyPermissions.hasPermissions(this, requiredPermissions)) {
            EasyPermissions.requestPermissions(this, "请允许应用获取相关权限", REQUEST_CODE_PERMISSION, requiredPermissions);
        } else {
            ContextKit.navigatePageTo(this, MainActivity.class);
            finish();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        // 检查是否所有必需权限都已授予
        List<String> requiredPermissions = new ArrayList<>(Arrays.asList(BASE_PERMISSIONS));
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
            requiredPermissions.addAll(Arrays.asList(STORAGE_PERMISSIONS));
        }

        boolean allGranted = true;
        for (String permission : requiredPermissions) {
            if (!perms.contains(permission)) {
                allGranted = false;
                break;
            }
        }

        if (allGranted) {
            ContextKit.navigatePageTo(this, MainActivity.class);
            finish();
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        for (String perm : perms) {
            Log.d(TAG, "onPermissionsDenied: " + perm);
        }
    }
}
